package com.spider.moudle.api.realize;

import com.spider.moudle.api.Crawler;
import com.spider.moudle.api.Downloader;
import com.spider.moudle.factory.DownloaderFactory;
import com.spider.moudle.handler.DataHandler;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Linbo Hu on 2017/12/18.
 * 标准的抓取器
 */
public abstract class AbstractCrawler implements Crawler {


    /**
     * 日志记录(必须)
     */
    protected Log log = LogFactory.getLog(this.getClass() + "_" + this.hashCode());

    /**
     * 这个downloader是用来发送请求的，且必须用工厂方法创建，工厂方法加了代理，不能自己new(必须)
     */
    protected Downloader downloader = DownloaderFactory.buildDownloader();

    /**
     * 时间的正则表达式匹配
     */
    private Pattern timePattern = Pattern.compile("(\\d{1,4}[-|\\/|年|\\.]\\d{1,2}[-|\\/|月|\\.]\\d{1,2}([日|号])?(\\s)*(\\d{1,2}([点|时])?((:)?\\d{1,2}(分)?((:)?\\d{1,2}(秒)?)?)?)?(\\s)*(PM|AM)?)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    /**
     * 数据处理
     */
    protected DataHandler dataHandler = new DataHandler();

    /**
     *
     * @param param
     * @return
     */
    protected Map<String, Object> getUrlParams(String param) {
        Map<String, Object> map = new HashMap<String, Object>(0);
        if (StringUtils.isBlank(param)) {
            return map;
        }
        String[] params = param.split("\\?")[1].split("&");
        for (int i = 0; i < params.length; i++) {
            String[] p = params[i].split("=");
            if (p.length == 2) {
                map.put(p[0], p[1]);
            }
        }
        return map;
    }

    /**
     * 获取时间字符串
     * @param content
     * @return
     */
    protected String getTime(String content) {
        try {
            List matches;
            Matcher matcher = timePattern.matcher(content);
            if (matcher.find() && matcher.groupCount() >= 1) {
                matches = new ArrayList();
                for (int i = 1; i <= matcher.groupCount(); i++) {
                    String temp = matcher.group(i);
                    matches.add(temp);
                }
            } else {
                matches = Collections.EMPTY_LIST;
            }
            if (matches.size() > 0) {
                return ((String) matches.get(0)).trim();
            } else {
            }
        } catch (Exception e) {
            return "";
        }
        return content;
    }
}
